﻿
Imports System.Data.Common

Namespace Services

    Public Class BenachrichtigungsService
        Inherits LibMultiPlug.Core.Services.ServiceDefinitions.BenachrichtigungsService


        Private Function GetLastInsertId() As Integer

            Dim cmd As DbCommand = _dataService.Factory.CreateCommand()

            cmd.CommandText = "SELECT last_insert_rowid()"

            Return CInt(_dataService.ExecuteScalar(cmd))

        End Function


        Public Sub New(ByVal dataService As DbDataLib.DbDataService)

            MyBase.New(dataService)

        End Sub


        Public Overloads Overrides Sub DeleteBenachrichtigung(ByVal id As Integer)

            Dim cmd As DbCommand = _dataService.Factory.CreateCommand()
            Dim sb As New Text.StringBuilder

            sb.AppendLine("DELETE FROM benachrichtigungen WHERE b_id = @p_id ")

            cmd.CommandText = sb.ToString()
            cmd.Parameters.Add(_dataService.CreateParameter("p_id", DbType.Int32, id))

            _dataService.ExecuteNonQuery(cmd)

        End Sub

        Public Overloads Overrides Sub DeleteBenachrichtigung(ByVal b As LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung)

            Call DeleteBenachrichtigung(b.Id)

        End Sub

        Public Overrides Function GetBenachrichtigungById(ByVal id As Integer) As LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung

        End Function

        Public Overrides Function GetBenachrichtigungen() As System.Collections.Generic.List(Of LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung)
            Dim lob As New List(Of LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung)

            Dim cmd As DbCommand = _dataService.Factory.CreateCommand()

            cmd.CommandText = "select * from benachrichtigungen order by b_datum desc"

            Dim dt As DataTable = _dataService.GetDataTable(cmd)


            For i As Integer = 0 To dt.Rows.Count - 1 Step 1

                Dim b As LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung

                Dim t As Type = Nothing

                t = System.Type.GetType(dt.Rows(i).Item("b_type").ToString())

                If t Is Nothing Then _
                    Continue For

                b = CType(Utils.DataUtils.DeserializeFromXmlString(dt.Rows(i).Item("b_xml").ToString(), t), Core.Benachrichtigungen.Benachrichtigung)

                lob.Add(b)

            Next


            Return lob

        End Function

        Public Overrides Sub SaveBenachrichtigung(ByVal b As LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung)

            If UpdateBenachrichtigung(b) < 1 Then _
                InsertBenachrichtigung(b)

        End Sub

        Private Function InsertBenachrichtigung(ByVal b As LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung) As Integer

            Dim cmd As DbCommand = _dataService.Factory.CreateCommand()
            Dim sb As New Text.StringBuilder

            sb.AppendLine("INSERT INTO benachrichtigungen(b_datum, b_type, b_xml) VALUES(@p_datum, @p_type, @p_xml) ")

            cmd.CommandText = sb.ToString()

            cmd.Parameters.Add(_dataService.CreateParameter("p_datum", DbType.DateTime, b.Datum))
            cmd.Parameters.Add(_dataService.CreateParameter("p_type", DbType.String, b.GetType().AssemblyQualifiedName.ToString()))
            cmd.Parameters.Add(_dataService.CreateParameter("p_xml", DbType.String, Utils.DataUtils.SerializeToXmlString(b)))

            _dataService.ExecuteNonQuery(cmd)

            b.Id = GetLastInsertId()

            Return UpdateBenachrichtigung(b)

        End Function

        Private Function UpdateBenachrichtigung(ByVal b As LibMultiPlug.Core.Benachrichtigungen.Benachrichtigung) As Integer

            Dim cmd As DbCommand = _dataService.Factory.CreateCommand()
            Dim sb As New Text.StringBuilder

            sb.AppendLine("UPDATE benachrichtigungen SET b_datum = @p_datum, ")
            sb.AppendLine(" b_type = @p_type, b_xml = @p_xml ")
            sb.AppendLine(" WHERE b_id = @p_id ")

            cmd.CommandText = sb.ToString()

            cmd.Parameters.Add(_dataService.CreateParameter("p_id", DbType.Int32, b.Id))
            cmd.Parameters.Add(_dataService.CreateParameter("p_datum", DbType.DateTime, b.Datum))
            cmd.Parameters.Add(_dataService.CreateParameter("p_type", DbType.String, b.GetType().AssemblyQualifiedName.ToString()))
            cmd.Parameters.Add(_dataService.CreateParameter("p_xml", DbType.String, Utils.DataUtils.SerializeToXmlString(b)))

            Return _dataService.ExecuteNonQuery(cmd)

        End Function




    End Class

End Namespace